﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace HYElectricSetting
{
	public class WireData
	{
		public static WireMaterialType GetWireMaterialType(Document doc, string name)
		{
			bool flag = doc == null || string.IsNullOrEmpty(name);
			WireMaterialType result;
			if (flag)
			{
				result = null;
			}
			else
			{
				WireMaterialType wireMaterialType = null;
				WireMaterialTypeSet wireMaterialTypes = doc.Settings.ElectricalSetting.WireMaterialTypes;
				WireMaterialTypeSetIterator wireMaterialTypeSetIterator = wireMaterialTypes.ReverseIterator();
				while (wireMaterialTypeSetIterator.MoveNext())
				{
					object obj = wireMaterialTypeSetIterator.Current;
					wireMaterialType = (obj as WireMaterialType);
					bool flag2 = name == wireMaterialType.Name;
					if (flag2)
					{
						return wireMaterialType;
					}
				}
				result = doc.Settings.ElectricalSetting.AddWireMaterialType(name, wireMaterialType);
			}
			return result;
		}

		public static TemperatureRatingType GetTemperatureRating(WireMaterialType wmt, string name)
		{
			bool flag = wmt == null || string.IsNullOrEmpty(name);
			TemperatureRatingType result;
			if (flag)
			{
				result = null;
			}
			else
			{
				TemperatureRatingType temperatureRatingType = null;
				TemperatureRatingTypeSet temperatureRatings = wmt.TemperatureRatings;
				TemperatureRatingTypeSetIterator temperatureRatingTypeSetIterator = temperatureRatings.ReverseIterator();
				while (temperatureRatingTypeSetIterator.MoveNext())
				{
					object obj = temperatureRatingTypeSetIterator.Current;
					temperatureRatingType = (obj as TemperatureRatingType);
					bool flag2 = name == temperatureRatingType.Name;
					if (flag2)
					{
						return temperatureRatingType;
					}
				}
				result = wmt.AddTemperatureRatingType(name, temperatureRatingType);
			}
			return result;
		}

		public static InsulationType GetInsulationType(TemperatureRatingType trt, string name)
		{
			bool flag = trt == null || string.IsNullOrEmpty(name);
			InsulationType result;
			if (flag)
			{
				result = null;
			}
			else
			{
				InsulationTypeSet insulationTypes = trt.InsulationTypes;
				InsulationTypeSetIterator insulationTypeSetIterator = insulationTypes.ReverseIterator();
				while (insulationTypeSetIterator.MoveNext())
				{
					object obj = insulationTypeSetIterator.Current;
					InsulationType insulationType = obj as InsulationType;
					bool flag2 = name == insulationType.Name;
					if (flag2)
					{
						return insulationType;
					}
				}
				result = trt.AddInsulationType(name);
			}
			return result;
		}

		public static WireSize GetMaxWireSize(TemperatureRatingType trt)
		{
			bool flag = trt == null;
			WireSize result;
			if (flag)
			{
				result = null;
			}
			else
			{
				long num = 0L;
				WireSize wireSize = null;
				WireSizeSet wireSizes = trt.WireSizes;
				WireSizeSetIterator wireSizeSetIterator = wireSizes.ForwardIterator();
				while (wireSizeSetIterator.MoveNext())
				{
					object obj = wireSizeSetIterator.Current;
					WireSize wireSize2 = obj as WireSize;
					bool flag2 = wireSize2 == null;
					if (flag2)
					{
						break;
					}
					bool flag3 = num < wireSize2.Ampacity;
					if (flag3)
					{
						num = wireSize2.Ampacity;
						wireSize = wireSize2;
					}
				}
				result = wireSize;
			}
			return result;
		}

		public static WireConduitType GetWireConduitType(Document doc)
		{
			bool flag = doc == null;
			WireConduitType result;
			if (flag)
			{
				result = null;
			}
			else
			{
				WireConduitTypeSet wireConduitTypes = doc.Settings.ElectricalSetting.WireConduitTypes;
				WireConduitTypeSetIterator wireConduitTypeSetIterator = wireConduitTypes.ForwardIterator();
				while (wireConduitTypeSetIterator.MoveNext())
				{
					object obj = wireConduitTypeSetIterator.Current;
					WireConduitType wireConduitType = obj as WireConduitType;
					bool flag2 = wireConduitType == null;
					if (flag2)
					{
						break;
					}
					bool flag3 = wireConduitType.Name == "非磁性";
					if (flag3)
					{
						return wireConduitType;
					}
				}
				result = null;
			}
			return result;
		}
	}
}
